import { createRouter, createWebHashHistory } from 'vue-router'
import Home from 'views/home/index.vue'

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'Home',
    meta: {
      isAuthToken: false,
      keepAlive: false,
      title: "首页"
    },
    component: Home
  },
  {
    path: '/Category',
    name: 'Category',
    meta: {
      isAuthToken: true,
      keepAlive: false,
      title: "分类"
    },
    component: () => import('views/Category/index.vue')
  },
  {
    path: '/detail',
    name: 'detail',
    meta: {
      isAuthToken: false,
      keepAlive: false,
      title: "商品详情"
    },
    component: () => import('views/detail/index.vue')
  },
  {
    path: '/profile',
    name: 'profile',
    meta: {
      isAuthToken: false,
      keepAlive: false,
      title: "用户中心"
    },
    component: () => import('views/profile/index.vue')
  },
  {
    path: '/shopCart',
    name: 'shopCart',
    meta: {
      isAuthToken: false,
      keepAlive: false,
      title: "购物车"
    },
    component: () => import('views/shopCart/index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  let token = window.localStorage.getItem("token")
  // console.log(to.meta.isAuthToken)
  if (to.meta.isAuthToken && !token) {
    // 需要 token
      return next("/profile")
  } else {
    next()
  }
})

export default router
